/*********************************************************************
 *                                                                   *
 *  Js Css Minifier ( JsCssMin )                                     *
 *  Author: Igor Kanshyn                                             *
 *  Copyright (c) 2009, Igor Kanshyn. All rights reserved.           *
 *                                                                   *
 *  JsCssMin is released under the Apache Software License.          *
 *  See LICENSE.txt for more details.                                *
 *                                                                   *
 *********************************************************************/

package org.grenader.jscssmin;

import org.grenader.jscssmin.files.FileUtils;
import org.grenader.jscssmin.files.ResourceTmpFileIterator;
import org.grenader.jscssmin.integrations.JsMinIntegration;
import org.grenader.jscssmin.model.Resource;
import org.grenader.jscssmin.model.ResourceGroup;

import java.io.File;
import java.io.IOException;

/**
 * <p>Product: JsCssMin ( JavaScript and CSS Minifier )<br>
 * Author: Igor Kanshyn (grenader).
 * </p>
 * Date: Apr 26, 2009
 */
public class JsCssMinProcessor extends JsCssProcessor {
    private ResourceGroup currentGroup;
    private File tmpDir;
    private ProcessLogger errorLogger = new ProcessLogger();

    public JsCssMinProcessor(JsCssMinConfig config) {
        super(config);
        tmpDir = FileUtils.createaTmpDir();
    }

    public void startGroup(ResourceGroup resourceGroup) {
        currentGroup = resourceGroup;
    }

    public boolean processResource(Resource resource) {
        File resourceFile = new File(getConfig().getCurrentDirAbsolutePath(), resource.getFileName());
        File tmpResultFile = new File(tmpDir, resource.getFileName());
        if (JsMinIntegration.processResourceFile(resourceFile, tmpResultFile, errorLogger)) {
            resource.setTmpFile(tmpResultFile.getAbsolutePath());
            return true;
        }

        return false;
    }

    public void processGroupsResource(Resource resource) {
        // todo: create group file
    }


    public void commitGroup(ResourceGroup resourceGroup) {
        if (currentGroup != resourceGroup)
            throw new IllegalStateException("Another group is trying to be commited");

        try {
            FileUtils.copyFilesIntoOne(new File(getConfig().getDestDirAbsolutePath(), currentGroup.getGroupFileName(getConfig().getVersion())), new ResourceTmpFileIterator(currentGroup));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String readFileContent(File file) {
        if (!file.canRead())
            throw new IllegalArgumentException("JsCssMin has not been created. Given file is inaccessible!");
        try {
            return FileUtils.readFileContent(file);
        } catch (IOException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("JsCssMin has not been created. Given file can't be read!");
        }

    }

    public void writeResultsIntoFile(File file, String content) {
        try {
            FileUtils.writeContentIntoFile(new File(getConfig().getDestDirAbsolutePath(), file.getName()), content);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void finishProcessing() {
        try {
            //
        }
        finally {
            FileUtils.deleteDirectory(tmpDir);
        }

    }
}
